package de.koller.worlders.engine.entity.mo.vehicle;

import de.koller.oceanFramework.math.Vec;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.util.Stat;
import de.koller.worlders.engine.entity.IAABB;
import de.koller.worlders.engine.entity.mo.AbstractPlayer;
import de.koller.worlders.engine.entity.mo.MovingObject;
import de.koller.worlders.gameplay.IGameplayPlayerObject;
import de.koller.worlders.render.VisualMap;

public class Ufo extends AbstractPlayer implements IGameplayPlayerObject {

	public static final float MAX_GEAR_VAL = 1;
	@Child private final UfoSteer ufosteer = new UfoSteer();
	private final UfoGui ugui = new UfoGui( this );
	
	private final Stat thrust = new Stat();
	protected final Vec visualThrottleSize;
	private float gearVal = 0;
	
	private float dirVelocity = 0;
	private float vehicleDir = -(float)Math.PI/2;
	
	public Ufo( float x, float y ) {
		super( x, y, 1f * 1.3f, 26f/32f * 1.3f );
		visualThrottleSize = new Vec( 1f*1.3f, 30f/32f*1.3f );
		thrust.getValues().put( "default", -MovingObject.GRAVITY * 1.2 );
	}

	@Override
	protected void init() {
		super.init();
		
		this.setVisual( VisualMap.ufoPlain );
		this.mostate.setGravityAffected( true );
		this.mostate.setGameplayAffected( true );
	
		this.wroot.rendering.addGUIElement( ugui );
		this.plattformHandler.setResetXenergie( false );
		
		this.reset();
	}

	@Override
	protected void release() {
		super.release();
		
		this.wroot.rendering.removeGUIElement( ugui );
	}
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
//		dirVelocity += gearVal * (elapsedTime/1000f);
//		vehicleDir += dirVelocity * (elapsedTime/1000f);
		
		Vec tf = new Vec( gearVal * (elapsedTime/1000f) * 0.0001, 0 );
		if( gearVal > 0)
			tf.setDirection( vehicleDir + Math.PI*.5f );
		else
			tf.setDirection( vehicleDir - Math.PI*.5f );
		
		if( !this.isBottomContact() )
			this.getSelfForce().add( tf );
		else
			this.getEnergy().setX( 0 );
		
		this.setVisualRotation( (float)Math.toDegrees( -vehicleDir ) - 90 );
	}
	
	@Override
	public void renderGL( int elapsedTime ) {
		super.renderGL( elapsedTime );
		
//		VisualMap.direction.setRenderColor( 0xff00ff00 );
//		Vec r = new Vec( this.getSize().getLength() / 1.6f, 0 );
//		r.setDirection( gearDirection );
//		
//		Vec p = this.getCentre().add( r );
//		
//		VisualMap.direction.setRotation( (float)Math.toDegrees( gearDirection ) - 90 );
//		VisualMap.direction.renderObject( p.getXf()-.5f, p.getYf()-.5f, 1, 1, 5, false );	
	}
	
	
	public void setThrottle( boolean on ) {
		if( on ) {
			this.setVisual( VisualMap.ufoThrottle );
			this.setVisualSize( this.visualThrottleSize );
	
			Vec tf = new Vec( thrust.valueF(), 0 );
			tf.setDirection( vehicleDir );
		
			this.getSelfForce().add( tf );
		} else {
			this.setVisual( VisualMap.ufoPlain );
			this.setVisualSize( null );
		}	
	}
	
	@Override
	public IAABB getAABB() {
		return this;
	}

	@Override
	public float getScreenAngle() {
		return 0;
	}
	
	public void gear( float dr ) {
		this.gearVal += dr;
		
		if( this.gearVal > MAX_GEAR_VAL )
			this.gearVal = MAX_GEAR_VAL;
		if( this.gearVal < -MAX_GEAR_VAL )
			this.gearVal = -MAX_GEAR_VAL;
	}

	public void setVehicleDir( float vehicleDir ) {
		this.vehicleDir = vehicleDir;
	}

	public void setDirVelocity( float dirVelocity ) {
		this.dirVelocity = dirVelocity;
	}
	
	public float getGearVal() {
		return gearVal;
	}
	
}
